<?php

/**
 * @example example bpdev_framework_init($id, $slug);
 */
function bpdev_framework_init($id, $slug) {

}

function bpdev_framework_addmeta( $stuff_id, $meta_key, $meta_value, $stuff_table, $table, $meta_table = 'meta_value' ) {

	global $wpdb, $bp;
	
	if ( !is_numeric( $stuff_id ) or ( !$stuff_id && !$meta_value ) )
		return false;

	if ( !$stuff_id && $meta_value ) {

		$stuff_id = $meta_value;

	}

	$meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
	$meta_value = (string)$meta_value;

	if ( is_string( $meta_value ) )
		$meta_value = stripslashes( $wpdb->escape( $meta_value ) );
	
	$meta_value = maybe_serialize( $meta_value );
	
	if ( empty( $meta_value ) ) {
		return bpdev_framework_deletemeta( $stuff_id, $meta_key, $meta_value, $stuff_table, $table, $meta_table );
	}

	$cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$stuff_table} = %d AND meta_key = %s AND {$meta_table} = %s", $stuff_id, $meta_key, $meta_value ) );

	if ( !$cur ) {
		$wpdb->query( $wpdb->prepare( "INSERT INTO {$table} ( {$stuff_table}, meta_key, {$meta_table} ) VALUES ( %d, %s, %s )", $stuff_id, $meta_key, $meta_value ) );
	} else {
		return false;
	}

	// TODO need to look into using this.
	// wp_cache_delete($user_id, 'users');

	return true;	

}

function bpdev_framework_updatemeta( $stuff_id, $meta_key, $meta_value, $stuff_table, $table, $meta_table = 'meta_value' ) {

	global $wpdb, $bp;
	
	if ( !is_numeric( $stuff_id ) or ( !$stuff_id && !$meta_value ) )
		return false;

	if ( !$stuff_id && $meta_value ) {

		$stuff_id = $meta_value;

	}

	$meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
	$meta_value = (string)$meta_value;

	if ( is_string( $meta_value ) )
		$meta_value = stripslashes( $wpdb->escape( $meta_value ) );
	
	$meta_value = maybe_serialize( $meta_value );
	
	if ( empty( $meta_value ) ) {
		return bpdev_framework_deletemeta( $stuff_id, $meta_key, $meta_value, $stuff_table, $table, $meta_table );

	}

	$cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE {$stuff_table} = %d AND meta_key = %s", $stuff_id, $meta_key ) );

	if ( !$cur ) {
		$wpdb->query( $wpdb->prepare( "INSERT INTO {$table} ( {$stuff_table}, meta_key, {$meta_table} ) VALUES ( %d, %s, %s )", $stuff_id, $meta_key, $meta_value ) );
	} else if ( $cur->meta_value != $meta_value ) {
		$wpdb->query( $wpdb->prepare( "UPDATE {$table} SET {$meta_table} = %s WHERE {$stuff_table} = %d AND meta_key = %s", $meta_value, $stuff_id, $meta_key ) );
	} else {
		return false;
	}

	// TODO need to look into using this.
	// wp_cache_delete($user_id, 'users');

	return true;	

}

function bpdev_framework_deletemeta( $stuff_id = false, $meta_key = false, $meta_value = false, $stuff_table, $table, $meta_table = 'meta_value' ) {
	global $wpdb, $bp;
	
	if ( !is_numeric( $stuff_id ) or ( !$stuff_id && !$meta_value ) )
		return false;

	if ( !$stuff_id && $meta_value ) {

		$stuff_id = $meta_value;

	}

	$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);

	if ( is_array($meta_value) || is_object($meta_value) )
		$meta_value = serialize($meta_value);
		
	$meta_value = trim( $meta_value );

	if ( !$meta_key ) {
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$stuff_table} = %d", $stuff_id ) );		
	} else if ( !$meta_value ) {
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$stuff_table} = %d AND meta_key = %s AND {$meta_table} = %s", $stuff_id, $meta_key, $meta_value ) );
	} else {
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE {$stuff_table} = %d AND meta_key = %s", $stuff_id, $meta_key ) );
	}
	
	// TODO need to look into using this.
	// wp_cache_delete($stuff_id, 'groups');

	return true;
}

function bpdev_framework_getmeta(  $stuff_id = false, $meta_key = null, $meta_value = false, $stuff_table, $table, $meta_table = 'meta_value' ) {
	global $wpdb, $bp;
	
	$stuff_id = (int) $stuff_id;

	if ( !$stuff_id && !$meta_key )
		return false;

	if ( !$stuff_id && $meta_value ) {

		$stuff_id = $meta_value;
		$meta_value = false;

	}

	if ( !empty( $meta_key ) ) {

		$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);

		if ( !$meta_value )
			$metas = $wpdb->get_col( $wpdb->prepare( "SELECT {$meta_table} FROM {$table} WHERE {$stuff_table} = %d AND meta_key = %s", $stuff_id, $meta_key ) );
		else 
			$metas = $wpdb->get_col( $wpdb->prepare( "SELECT {$meta_table} FROM {$table} WHERE {$stuff_table} = %d AND {$meta_table} = %s", $stuff_id, $meta_value ) );

	} else {

		$metas = $wpdb->get_col( $wpdb->prepare( "SELECT {$meta_table} FROM {$table} WHERE {$stuff_table} = %d", $stuff_id ) );

	}
	if ( empty($metas) ) {
		if ( empty($meta_key) )
			return array();
		else
			return '';
	}

	$metas = array_map('maybe_unserialize', $metas);

	if ( 1 == count($metas) )
		return $metas[0];
	else
		return $metas;
}

/**
 * @example example bpdev_framework_add_page('my_setting_title', 'my_setting_title');
 */
function bpdev_framework_add_page($page_title, $page_content) {

	add_action( 'bp_template_title', $page_title );
	add_action( 'bp_template_content', $page_content );
//	add_action( 'bp_template_content_header', 'flickr_option_tabs' );
	bp_catch_uri('plugin-template');

}


function bpdev_framework_title($slug, $title) {

	echo apply_filter( 'bpdev_framework_' . $slug . '_title' , $title);

}


function bpdev_framework_function() {

	do_action('bpdev_framework_' . $slug . '_function' , $slug);

}


function bpdev_framework_page_content() {

	do_action('bpdev_framework_' . $slug . '_content' , $slug);

}
?>